package tq.com.tqstudent.ui.activity.classes;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.lib.common.base.BaseActivity;
import com.lzy.okgo.OkGo;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

import butterknife.BindView;
import tq.com.tqstudent.App;
import tq.com.tqstudent.Constants;
import tq.com.tqstudent.R;
import tq.com.tqstudent.adapter.ShareListAdapter;
import tq.com.tqstudent.bean.BaseBean;
import tq.com.tqstudent.bean.MalistBean;
import tq.com.tqstudent.bean.event.ShareSuccessEvent;
import tq.com.tqstudent.net.ok.DialogCallback;
import tq.com.tqstudent.utils.VarietyUtil;
import tq.com.tqstudent.views.ShareListHeader;

/**
 * @Author rames
 * @date 2019/4/3 09:45
 */
public class ShareListActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty_layout)
    LinearLayout emptyLayout;
    private ShareListAdapter adapter;
    private ShareListHeader header;
    private int index;
    private int pageNO = 1;
    private int maxCount = 5;

    /**
     *
     * @param activity
     * @param index
     * @param maxCount 上传的上限值
     */
    public static void show(Activity activity, int index, int maxCount){
        Intent intent = new Intent(activity, ShareListActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("index", index);
        bundle.putInt("maxCount", maxCount);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    protected void getExtras(Bundle extras) {
        index = extras.getInt("index");
        maxCount = extras.getInt("maxCount");
    }

    @Override
    public int initLayout() {
        return R.layout.activity_recyclerview;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setTitle("分享");
        adapter = new ShareListAdapter(this);
        header = new ShareListHeader(this, index);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        adapter.addHeaderView(header);
        header.setValue("0", maxCount+"");

        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNO++;
                getData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNO = 1;
                getData();
            }
        });

        refreshLayout.autoRefresh();
        refreshLayout.setEnableLoadMoreWhenContentNotFull(false);
    }

    @Subscribe
    public void onShareSuccessEvent(ShareSuccessEvent event){
        refreshLayout.autoRefresh();
    }

    private void getData() {
        String uri = "api/clz/malist/"+index+"/"+ Constants.PAGE_SIZE+"/"+pageNO;
        OkGo.<BaseBean<ArrayList<MalistBean>>>get(App.getHttpUrl(uri))
                .tag(this)
                .execute(new DialogCallback<ArrayList<MalistBean>>(ShareListActivity.this) {
                    @Override
                    public void handleSuccess(ArrayList<MalistBean> data) {
                        // 这是加载更多
                        if(refreshLayout.getState() == RefreshState.Loading){
                            adapter.addData(data);
                            refreshLayout.finishLoadMore();
                            if(data.size() < Constants.PAGE_SIZE){
                                refreshLayout.setEnableLoadMore(false);
                            }

                            // 这是下拉刷新
                        } else {
                            adapter.getDatas().clear();
                            adapter.setData(data);
                            refreshLayout.finishRefresh();
                            refreshLayout.setEnableLoadMore(true);
                        }

                        header.setValue(""+data.size(), maxCount+"");
                        emptyLayout.setVisibility(recyclerView.getAdapter().getItemCount() > 0 ? View.GONE : View.VISIBLE);
                    }

                    @Override
                    public void handleFail(BaseBean body) {
                        super.handleFail(body);
                        refreshLayout.finishLoadMore();
                        refreshLayout.finishRefresh();
                    }
                });
    }

    @Override
    public void initData() {
//        getData();
    }
}
